﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TSCC.DataAccess;
using System.Data;

namespace TSCC.Service
{
    public class Service_ExceptionLog
    {
        protected DA_ExceptionLog _da = null;

        protected DA_ExceptionLog da
        {
            get
            {
                if (_da == null)
                {
                    _da = new DA_ExceptionLog();
                }
                return _da;
            }
        }

        protected string msg = string.Empty;
        public string MSG
        {
            get
            {
                return msg;
            }
        }

        public bool Insert(string p_員編, string p_姓名, string p_組別
                                , string p_例外項目, string p_例外時間起, string p_例外時間迄
                              , int p_實際產能, int p_聯絡未果, string p_備註,string p_csrDate)
        {

            int result = 0;
            if (ValidateStartEndTime(p_例外時間起, p_例外時間迄))
            {

                int outTime = CalOutTime(p_例外時間起, p_例外時間迄);
                result = da.Insert(p_員編, p_姓名, p_組別, p_例外項目
                            , p_例外時間起, p_例外時間迄, p_實際產能
                            , p_聯絡未果, p_備註,p_csrDate,outTime);
                msg = result < 0 ? da.MSG : string.Empty;
            }
            return result.Equals(1) ? true : false;
        }

        public bool Update(string p_員編, string p_姓名
                                , string p_例外項目, string p_例外時間起, string p_例外時間迄
                              , int p_實際產能, int p_聯絡未果, string p_備註, string p_csrDate)
        {

            int result = 0;
            if (ValidateStartEndTime(p_例外時間起, p_例外時間迄))
            {

                result = da.Update(p_員編, p_姓名, p_例外項目, p_例外時間起, p_例外時間迄, p_實際產能
                    , p_聯絡未果, p_備註, p_csrDate);
                msg = result < 0 ? da.MSG : string.Empty;
            }
            return result.Equals(1) ? true : false;
        }


        public bool Delete(string p_員編, string p_例外時間起, string p_csrDate)
        {
            int result = 0;
            result = da.Delete(p_員編, p_例外時間起, p_csrDate);
            msg = result < 0 ? da.MSG : string.Empty;
            return result.Equals(1) ? true : false;
        }

        public DataTable GetData(string p_csrNO, string p_startDate, string p_endDate)
        {
            

            return da.Query(p_csrNO, p_startDate, p_endDate);
        }

        /// <summary>
        /// 檢查通過為true
        /// </summary>
        /// <param name="p_start"></param>
        /// <param name="p_end"></param>
        /// <returns></returns>
        public bool ValidateStartEndTime(string p_start, string p_end)
        {
            bool ok = false;

            ok = Utility.EndGreatThanStart(p_start, p_end);
            return ok;
        }

        public bool ExceptionLogIsExist(string p_CSRNo, string p_csrDate, string p_例外時間起)
        {
            return false;
        }

        /// <summary>
        /// 計算開始到結束共多少時間
        /// </summary>
        /// <param name="p_strat"></param>
        /// <param name="p_end"></param>
        /// <returns></returns>
        public int CalOutTime(string p_strat, string p_end)
        {
            DateTime t1, t2;
            t1 = Utility.GetDateTime(p_strat);
            t2 = Utility.GetDateTime(p_end);
            TimeSpan ts = t2 - t1;
            return Convert.ToInt32(ts.TotalMinutes) ;
        }
    
    }
}
